package com.blb.day20231102springbootdemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.day20231102springbootdemo.pojo.Score;
import com.blb.day20231102springbootdemo.pojo.Student;
import com.blb.day20231102springbootdemo.service.ScoreService;
import com.blb.day20231102springbootdemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("findScores")
    public String findScores(Model model){
        List<Score> scores = scoreService.getAll();
        model.addAttribute("scores",scores);
        return "scores";
    }

    @RequestMapping("findScoresPage")
    public String findScoresPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                 @RequestParam(required = false,defaultValue = "5") Long size,
                                 Model model){
        IPage<Score> page = scoreService.getScorePage(current, size);
        model.addAttribute("page",page);
        return "scores";
    }

    @RequestMapping("toUpdateScore")
    public String toUpdateScore(Long id,Model model){
        Score score = scoreService.getById(id);
        List<Student> students = studentService.list();
        model.addAttribute("score",score);
        model.addAttribute("students",students);
        return "update-score";
    }

    @RequestMapping("toSaveScore")
    public String toSaveScore(Model model){
        List<Student> students = studentService.list();
        model.addAttribute("students",students);
        return "add-score";
    }

    @RequestMapping("doUpdate")
    public String doUpdate(Score score){
        scoreService.updateById(score);
        return "redirect:findScores";
    }

    @RequestMapping("doDelete")
    public String doDelete(Long id){
        scoreService.removeById(id);
        return "redirect:findScores";
    }

    @RequestMapping("doSave")
    public String doSave(Score score){
        scoreService.save(score);
        return "redirect:findScores";
    }
}
